package jp.ac.waseda.cs.washi.game.scene;

import jp.ac.waseda.cs.washi.game.GameEnvironment;
import jp.ac.waseda.cs.washi.game.GameManager;
import jp.ac.waseda.cs.washi.game.api.PlayerAction;
import jp.ac.waseda.cs.washi.game.entity.Chara;
import jp.ac.waseda.cs.washi.game.factory.MapFactory;
import jp.ac.waseda.cs.washi.gameaiarena.api.Direction4;
import jp.ac.waseda.cs.washi.gameaiarena.api.Point2;
import jp.ac.waseda.cs.washi.gameaiarena.gui.Renderer;
import jp.ac.waseda.cs.washi.gameaiarena.key.MappedInputer;
import jp.ac.waseda.cs.washi.gameaiarena.scene.Scene;

public class UserScene extends GameScene {

	private PlayerAction[] _actions;

	@Override
	public void initialize(GameEnvironment env) {
		Renderer render = env.getRenderer();
		int playerCount = 2;
		_gameManager = new GameManager(render, playerCount,
				MapFactory.getRandomMapIndex(), 1000 * 10);
		_actions = new PlayerAction[playerCount];
		render.waitLoadImage();
	}

	@Override
	public Scene<GameEnvironment> run(GameEnvironment env) {
		Chara[] charas = _gameManager.getCharas();
		for (Point2 p : charas[0].getInfo().getMiniPumpkinCenterLocations()) {
			System.out.println(p);
		}
		
		Direction4[] directions = Direction4.values();
		PlayerAction[] playerActions = PlayerAction.values();
		MappedInputer inputer = env.getInputer();

		for (int iChara = 0; iChara < charas.length; iChara++) {
			int diff = iChara * 6;
			_actions[iChara] = PlayerAction.NONE;
			if (inputer.isPress(4 + diff)) {
				_actions[iChara] = PlayerAction.SPELL;
				continue;
			}
			if (inputer.isPress(5 + diff)) {
				_actions[iChara] = PlayerAction.CLEAR;
				continue;
			}
			for (int iDirections = 0; iDirections < directions.length; iDirections++) {
				if (inputer.isPress(iDirections + diff)) {
					_actions[iChara] = playerActions[iDirections];
					break;
				}
			}
		}
		
		_gameManager.advanceForDebug(_actions);

		return this;
	}
}
